//
//  XBookkeepingTableViewCell.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/23.
//

#import "XBookkeepingTableViewCell.h"
#import "XDynamicModel.h"

@interface XBookkeepingTableViewCell ()

@property(nonatomic,weak)MyRelativeLayout* contentLayout;

@property(nonatomic,weak)UIImageView* typeImage;

@property(nonatomic,weak)UILabel* typeLb;

@property(nonatomic,weak)UILabel* amtLb;

@end

@implementation XBookkeepingTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){

        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self initSubViews];
    }
    return self; 
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)initSubViews{
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.padding = UIEdgeInsetsMake(0, 16,0, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = PX(50);
    contentLayout.subviewHSpace = 10;
    contentLayout.cacheEstimatedRect = YES;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    UIImageView* typeImage = [[UIImageView alloc] init];
    typeImage.myLeft = 0;
    typeImage.myCenterY = 0;
    typeImage.mySize = CGSizeMake(PX(30), PX(30));
    [contentLayout addSubview:typeImage];
    _typeImage = typeImage;
    
    UILabel* typeLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_BLACK_DARK text:@"--"];
    typeLb.leftPos.equalTo(typeImage.rightPos).offset(10);
    typeLb.myCenterY = 0;
    [contentLayout addSubview:typeLb];
    _typeLb = typeLb;

    UILabel* amtLb = [UILabel lableWithFont:NUMBERFONT_FIT(15) color:COLOR_GREEN text:@"￥0.00"];
    amtLb.textAlignment = NSTextAlignmentRight;
    amtLb.myRight = amtLb.myCenterY = 0;
    [contentLayout addSubview:amtLb];
    _amtLb = amtLb;
}

- (void)setModel:(XDynamicModel *)model{
    _model = model;
    
    NSString* imageName;
    switch (model.feeType) {
        case XFeeTypeXZ:
            imageName = @"x_income_xz_n";
            break;
        case XFeeTypeHB:
            imageName = @"x_income_hb_n";
            break;
        case XFeeTypeJJ:
            imageName = @"x_income_jj_n";
            break;
        case XFeeTypeBX:
            imageName = @"x_income_bx_n";
            break;
        case XFeeTypeLC:
            imageName = @"x_income_lc_n";
            break;
        case XFeeTypeTK:
            imageName = @"x_income_tk_n";
            break;
        case XFeeTypeTZ:
            imageName = @"x_income_tz_n";
            break;
        case XFeeTypeJZ:
            imageName = @"x_income_jz_n";
            break;
        case XFeeTypeGC:
            imageName = @"x_income_gc_n";
            break;
        case XFeeTypeQT1:
            imageName = @"x_income_qt_n";
            break;
        case XFeeTypeCY:
            imageName = @"x_expend_cy_n";
            break;
        case XFeeTypeFD:
            imageName = @"x_expend_fz_n";
            break;
        case XFeeTypeGW:
            imageName = @"x_expend_gw_n";
            break;
        case XFeeTypeHZ:
            imageName = @"x_expend_hz_n";
            break;
        case XFeeTypeRY:
            imageName = @"x_expend_ry_n";
            break;
        case XFeeTypeSD:
            imageName = @"x_expend_sd_n";
            break;
        case XFeeTypeXX:
            imageName = @"x_expend_xx_n";
            break;
        case XFeeTypeYY:
            imageName = @"x_expend_lr_n";
            break;
        case XFeeTypeSM:
            imageName = @"x_expend_sm_n";
            break;
        case XFeeTypeCW:
            imageName = @"x_expend_cw_n";
            break;
        case XFeeTypeTX:
            imageName = @"x_expend_tx_n";
            break;
        case XFeeTypeJT:
            imageName = @"x_expend_jt_n";
            break;
        case XFeeTypeGY:
            imageName = @"x_expend_gy_n";
            break;
        case XFeeTypeKD:
            imageName = @"x_expend_kd_n";
            break;
        case XFeeTypeSJ:
            imageName = @"x_expend_sj_n";
            break;
        case XFeeTypeYL:
            imageName = @"x_expend_yl_n";
            break;
        case XFeeTypeQT2:
            imageName = @"x_expend_qt_n";
            break;
        default:
            break;
    }
    
    self.typeImage.image = [UIImage imageNamed:imageName];
    [self.typeImage sizeToFit];
    
    self.typeLb.text = model.title;
    [self.typeLb sizeToFit];

    self.amtLb.text = [NSString stringWithFormat:@"%@￥%.2f",model.feeType > 200?@"-":@"+",model.feeAmt];
    self.amtLb.textColor = model.feeType > 200?COLOR_RED:COLOR_GREEN;
    [self.amtLb sizeToFit];
}

#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width;
        cellSize = [self.contentLayout sizeThatFits:size];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
